<template>
  <select class="client-sources" v-model="sourceValue">
    <option v-for="option in customSources" :value="option.key">{{ option.value }}</option>
  </select>
</template>

<script>
  import store from '../store'
  import {showDialog, hideDialog} from '../lib/dialog'
  import {cleanClient} from '../lib/client'

  export default {
    props: {
      client: {
        require: true
      }
    },
    data() {
      return {
        sourceValue: '',
        customSources: []
      }
    },
    components: {
    },
    watch: {
      sourceValue(value) {
        this.client.source_id = value
        if(this.alterId) clearTimeout(this.alterId)
        this.$vux.loading.show({
          text: '请稍等...'
        })
        this.alterId = setTimeout(() => {
          let data = cleanClient(this.client, this.loginUser)
          store.dispatch('saveClientInfo', data)
            .then((reseponse) => {
              this.$vux.loading.hide()
              if(reseponse.data.errcode === 0) {
                showDialog(store, reseponse.data.errmsg)
              } else {
                showDialog(store, '后台发生了点小问题，请联系管理员!')
              }
              setTimeout(() => {
                hideDialog(store)
              }, 1500)
            })
        }, 300)
      }
    },
    methods: {
      getCustomSource () {
        store.dispatch('getDictionary', 'custom_source')
          .then((response) => {
            let data = response.data
            if(data.errcode == 0) {
              let customSourcesData = data.results
              console.log(customSourcesData)
              for(let index in customSourcesData) {
                this.customSources.push({
                  key: index,
                  vaule: customSourcesData[index]
                })
              }
              console.log(this.customSources)
            }
          })
      }
    },
    computed: {
      customSources () {
        return this.$store.state.app.customSourcesOptions
      },
      loginUser () {
        return this.$store.state.app.user
      }
    },
    mounted () {
      this.getCustomSource()
      // showDialog(store, '后台发生了点小问题，请联系管理员!')
    }

  }
</script>

<style lang="less">
  .client-sources {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top:0;
    display: inline-block;
    opacity: 0;
  }

</style>
